package com.lh.music.datamanager;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.lh.music.entity.Song;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by a on 2016/12/12.
 */

public class DataManager {
    //初始化discover的中tab的string
    public static List<String>  getDiscoverTabTitle(){
        List<String>  list=new ArrayList<>();
        list.add(new String("个性推荐"));
        list.add(new String("歌单"));
        list.add(new String("主播电台"));
        list.add(new String("排行榜"));
        return  list;
    }
    //初始化friend的中tab的string
    public static List<String>  getFriendTabTitle(){
        List<String>  list=new ArrayList<>();
        list.add(new String("动态"));
        list.add(new String("附近"));
        list.add(new String("好友"));
        return  list;
    }
    //初始化localsong的中tab的string
    public static List<String>  getLocalSongTabTitle(){
        List<String>  list=new ArrayList<>();
        list.add(new String("单曲"));
        list.add(new String("歌手"));
        list.add(new String("专辑"));
        list.add(new String("文件夹"));
        return  list;
    }
    //通过内容提供者  访问sd卡上的音频文件
    private ContentResolver resolver=null;
    //本地音乐的uri
    private Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    //查询那几列
    private  String [] mProjection={
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.SIZE,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.ARTIST_ID,
            MediaStore.Audio.Media.ALBUM_ID
    };
    public DataManager(Context context) {
        this.context=context;
        resolver=context.getContentResolver();
    }
    private Context context;
    /**
     * 按照歌名排序
     * @return
     */
    public ArrayList<Song> findAllSong(){
        ArrayList<Song>  list=new ArrayList<>();
        Song song=null;
        Cursor cursor=resolver.query(uri, mProjection, null, null,MediaStore.Audio.Media.ARTIST);
        while (cursor.moveToNext()){
            song=new Song(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)),
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)),
                    cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) );
            song.setArtist_id(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)));
            song.setAlbum_id(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
            //找到歌曲对应的专辑对应的key
          /*  int musicColumnIndex = cursor
                    .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_KEY);
            String musicAlbumKey = cursor.getString(musicColumnIndex);
            String []argArr={musicAlbumKey};
            ContentResolver albumResolver = context.getContentResolver();
            Cursor albumCursor = albumResolver.query(
                    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
                    MediaStore.Audio.AudioColumns.ALBUM_KEY + " = ?",
                    argArr, null);
            if (null != albumCursor && albumCursor.getCount() > 0) {
                albumCursor.moveToFirst();
                int albumArtIndex = albumCursor
                        .getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);
                String musicAlbumArtPath = albumCursor.getString(albumArtIndex);
                if (null != musicAlbumArtPath
                        && !"".equals(musicAlbumArtPath)) {
                    song.setAlbum_img(musicAlbumArtPath);
                }
            }*/
            list.add(song);
        }
        return list;
    }

    /**
     * 按照歌手name进行排序
     * @return
     */
    public ArrayList<Song> findAllSongOrderBySinger(){
        ArrayList<Song>  list=new ArrayList<>();
        Song song=null;
        Cursor cursor=resolver.query(uri, mProjection, null, null, MediaStore.Audio.Media.ARTIST);
        while (cursor.moveToNext()){
            song=new Song(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)),
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)),
                    cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) );
            //找到歌曲对应的专辑对应的key
           /* String musicAlbumKey =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY));
            String []argArr={musicAlbumKey};
            ContentResolver albumResolver = context.getContentResolver();
            Cursor albumCursor = albumResolver.query(
                    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
                    MediaStore.Audio.AudioColumns.ALBUM_KEY + " = ?",
                    argArr, null);
            if (null != albumCursor && albumCursor.getCount() > 0) {
                albumCursor.moveToFirst();
                int albumArtIndex = albumCursor
                        .getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);
                String musicAlbumArtPath = albumCursor.getString(albumArtIndex);
                if (null != musicAlbumArtPath
                        && !"".equals(musicAlbumArtPath)) {
                    song.setAlbum_img(musicAlbumArtPath);
                }
            }*/
            list.add(song);
        }
        return list;
    }
    /**
     * 按照歌手name进行排序
     * @return
     */
    public ArrayList<Song> findAllSongOrderByAlbumt(){
        ArrayList<Song>  list=new ArrayList<>();
        Song song=null;
        Cursor cursor=resolver.query(uri, mProjection, null, null, MediaStore.Audio.Media.ALBUM);
        while (cursor.moveToNext()){
            song=new Song(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)),
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)),
                    cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) );
           /* //找到歌曲对应的专辑对应的key
            String musicAlbumKey =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_KEY));
            String []argArr={musicAlbumKey};
            ContentResolver albumResolver = context.getContentResolver();
            Cursor albumCursor = albumResolver.query(
                    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
                    MediaStore.Audio.AudioColumns.ALBUM_KEY + " = ?",
                    argArr, null);
            if (null != albumCursor && albumCursor.getCount() > 0) {
                albumCursor.moveToFirst();
                int albumArtIndex = albumCursor
                        .getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);
                String musicAlbumArtPath = albumCursor.getString(albumArtIndex);
                if (null != musicAlbumArtPath
                        && !"".equals(musicAlbumArtPath)) {
                    song.setAlbum_img(musicAlbumArtPath);
                }
            }*/
            list.add(song);
        }
        return list;
    }
    /**
     * 初始化排行的类型
     */
    public static List<String> getPaihangTypes(){
        List<String> list=new ArrayList<>();
        list.add(1+"");
        list.add(2+"");
        list.add(8+"");
        list.add(10+"");
        list.add(11+"");
        //list.add(12+"");
        list.add(20+"");
        list.add(21+"");
        list.add(22+"");
        list.add(23+"");
        list.add(24+"");
        list.add(25+"");
        return list;
    }
}
